// pages/mineInfoView/mineInfoView.js
var Api = require('../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

    infoList: [

      { "infoName": "称呼", "infoType": "text", "placeText": "昵称","value":""},
      { "infoName": "性别", "infoType": "radio", infoArray: [{"name": "male", "value": "男", "checked": true}, {"name": "female", "value": "女", "checked": false }]},
      { "infoName": "手机", "infoType": "number", "placeText": "*联系电话 非常重要", "value": "" },
      // { "infoName": "微信", "infoType": "text", "placeText": "" },
    ],
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    let app = getApp()
    let logged = app.globalData.logged
    let userInfo = null
    let infoList = self.data.infoList

    if (logged = true) {
      userInfo = app.globalData.userInfo

      console.log(userInfo)
      infoList[0].value = userInfo.name
      
      if (userInfo.gender == 1){
        infoList[1].infoArray[0].checked = true
        }else{
        infoList[1].infoArray[1].checked = true  
        }
      infoList[2].value = userInfo.telephone
        
      self.setData({ logged: logged, userInfo: userInfo, infoList: infoList })
    }
  },

  bindKeyInput: function (e){
    let inputIndex = e.currentTarget.dataset.inputindex
    let inputText = e.detail.value
    let self = this
    let infoList = self.data.infoList

    // console.log('input', infoList, e)
    for (var index in infoList){
      let model = infoList[index]
      if (index == inputIndex){
        model.value = inputText
      }
    }
    
    
    self.setData({ infoList: infoList})
    
  },

  radioChange: function (e) {
    
    let self = this
    let infoList = self.data.infoList
    let modelArray = infoList[1].infoArray
    let radioValue = e.detail.value
    for (var index in modelArray){
      let model = modelArray[index]
      if (model.name == radioValue){
        modelArray[index].checked = true
      }else{
        modelArray[index].checked = false
      }
    }

    infoList[1].infoArray = modelArray

    // console.log('radio', infoList, radioValue)

    self.setData({ infoList: infoList })

  },




  postInfoTapped:function(){
    let self = this
    let postUrl = Api.service.postRegister
    let app = getApp()
    let infoList = self.data.infoList
    let requestHead = app.globalData.requestHead
    if (requestHead.isHasHeadInfo == false) {
      app.selfLogin(self.postInfoTapped)
    }
    let head = requestHead.header
    console.log('head', head)

    var name = '000test'
    var sex = 'male'
    var telephone = '11222220'

    for (var index in infoList){
      let model = infoList[index]

      if (index == 0){
        name = model.value
      }else if (index == 1){

        let modelArray = model.infoArray
        for (var arrIndex in modelArray){
          let arrayModel = modelArray[arrIndex]
          if (arrayModel.checked == true){
            sex = arrayModel.name
          }
        }
        
      }else if (index == 2){
        telephone = model.value
      }
    }

    if (sex == 'male') {
      sex = 1
    }else{
      sex = 2
    }

    if (telephone.length != 11){
      wx.showToast({
        image: '/images/noWayWhite.png',
        title: '请输入11位手机号',
      })
      return
    }
    
    let para = {
      
      name: name,
      sex: sex,
      telephone: telephone

    }

   
    wx.request({
      url: postUrl,
      data: para,
      header: head,
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      success: function (res) {
        console.log("postres", res)

      }
    })
  },





  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})